﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using BaocaoNPC.common;
using BaocaoNPC.Usercontrols.reports;


namespace BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for NPC_BCTSKT.xaml
    /// </summary>
    public partial class NPC_BCTSKT : UserControl
    {
        public DataSet ds_bctskt = new DataSet();
        public Int16 i16ThangBC = 0;
        public Int16 i16NamBC = 0;
        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";
        int rowselect = 0;
        public NPC_BCTSKT()
        {
            try
            {
                InitializeComponent();
                GET_THANGNAM();

                if (i16ThangBC.ToString().Length == 1)
                {
                    dtpThangNam.Text = "0" + i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                }
                else
                {
                    dtpThangNam.Text = i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                }
                load_BCTSKT(strMa_dviqly, i16ThangBC, i16NamBC);
                btnGhi.IsEnabled = false;
                if(ds_bctskt!=null)
                {
                    if (ds_bctskt.Tables.Count > 0)
                    {
                        if (ds_bctskt.Tables["BCTSKT"].Rows.Count > 0)
                        {
                            load_grid();
                        }
                        else
                        {
                            Khoitao_BCTSKT(strMa_dviqly, i16ThangBC, i16NamBC, 0);
                            load_grid();
                        }
                    }
                    else
                    {
                        Khoitao_BCTSKT(strMa_dviqly, i16ThangBC, i16NamBC, 0);
                        load_grid();
                    }
                }

            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void btnHuy_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnGhi_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ds_bctskt.Tables.Count > 0)
                {
                    if (ds_bctskt.Tables["BCTSKT"].Rows.Count > 0)
                    {
                        //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                        //if (bus.update_bctskt(ds_bctskt))
                        if (GlobalServices.Service_NPCUS.update_bctskt(ds_bctskt))
                        { 
                            MessageBox.Show("Cập nhật kết quả thành công");
                            btnin.IsEnabled = true;
                        }
                        else 
                        {
                            MessageBox.Show("Lỗi trong quá trình ghi dữ liệu", "Báo lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void btnin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //ds_bctskt.Tables["BCTSKT"].WriteXmlSchema("D:\\Projects\\NPCUS\\NPC_BCTSKT.xsd");
                rpt_NPC_BCTSKT rpt = new rpt_NPC_BCTSKT();
                rpt.SetDataSource(ds_bctskt.Tables["BCTSKT"]);
                rpt.SetParameterValue("pTendienluc", strTen_dviqly);
                rpt.SetParameterValue("pTencongty", strTen_dviqly_CT);
                UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");

                //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void btnnhap_Click(object sender, RoutedEventArgs e)
        {

            btnGhi.IsEnabled = true;
            btnHuy.IsEnabled = true;
            btnin.IsEnabled = false;
        }

        private void btnload_thangtruoc_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Khoitao_BCTSKT(strMa_dviqly, i16ThangBC, i16NamBC, 1);
                load_grid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }
        private void load_BCTSKT(string strmadvi, short pthang, short pnam)
        {
            try
            {
                ds_bctskt = GlobalServices.Service_NPCUS.get_bctskt(strmadvi, pthang, pnam);
            
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }
        private void load_grid()
        {
            try
            {
                dgrBCTSKT.ItemsSource = null;
                dgrBCTSKT.Items.Clear();
                if (ds_bctskt != null)
                {
                    if (ds_bctskt.Tables.Count > 0)
                    {
                        if (ds_bctskt.Tables["BCTSKT"].Rows.Count > 0)
                        {
                            dgrBCTSKT.ItemsSource = ds_bctskt.Tables["BCTSKT"].DefaultView;
                            ds_bctskt.Tables["BCTSKT"].DefaultView.AllowNew = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }
        private void Khoitao_BCTSKT(string strmadvi, Int16 pthang,Int16 pnam, Int16 pthangtruoc)
        {
            try
            {
                ds_bctskt.Clear();
                ds_bctskt = GlobalServices.Service_NPCUS.khoitao_BCTSKT(strmadvi, pthang, pnam, pthangtruoc);
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }
        private void GET_THANGNAM()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach (DataRow dr in dsthanglviec.Tables[0].Rows)
                        if (dr["PHAN_HE"].ToString() == "BC" && dr["MA_DVIQLY"].ToString() == strMa_dviqly)
                        {
                            i16ThangBC = Convert.ToInt16(dr["THANG"]);
                            i16NamBC = Convert.ToInt16(dr["NAM"]);
                        }
                }

            }
            catch
            {
                i16ThangBC = Convert.ToInt16(DateTime.Now.Month);
                i16NamBC = Convert.ToInt16(DateTime.Now.Year);
            }
        }

        private void dgrBCTSKT_KeyDown(object sender, KeyEventArgs e)
        {

            try
            {
                rowselect = dgrBCTSKT.SelectedIndex;

            }
            catch
            {
 
            }
        }
        private decimal convettodec(Object a)
        {
            if (a.ToString() == "")
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(a);
            }
        }

        private void dgrBCTSKT_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                int index = dgrBCTSKT.SelectedIndex;
                DataRowView dgr = dgrBCTSKT.SelectedItem as DataRowView;
                DataRow[] dr = ds_bctskt.Tables["BCTSKT"].Select();

                if (e.Key == Key.Enter)
                {
                    if (dgrBCTSKT.CurrentColumn == dgrBCTSKT.Columns[11] || dgrBCTSKT.CurrentColumn == dgrBCTSKT.Columns[7] || dgrBCTSKT.CurrentColumn == dgrBCTSKT.Columns[9])
                    {
                        dr[rowselect].SetField("TSO_MBA", convettodec(dr[rowselect]["SOMBA_CD"].ToString()) + convettodec(dr[rowselect]["SOMBA_SC"]) + convettodec(dr[rowselect]["SOMBA_CC"]));
                        dgr.BeginEdit();
                    }
                    if (dgrBCTSKT.CurrentColumn == dgrBCTSKT.Columns[8] || dgrBCTSKT.CurrentColumn == dgrBCTSKT.Columns[10] || dgrBCTSKT.CurrentColumn == dgrBCTSKT.Columns[12])
                    {
                        dr[rowselect].SetField("TDL_MBA", convettodec(dr[rowselect]["DLMBA_CD"].ToString()) + convettodec(dr[rowselect]["DLMBA_SC"]) + convettodec(dr[rowselect]["DLMBA_CC"]));
                        dgr.BeginEdit();
                    }
                }

            }
            catch
            {

            }
        }
    }
}
